import config from './config'
import page from './page'
import storage from './storage'
import http from './http'
import crypto from './crypto'
import api from './api'
import env from './env'
import update from './update'

import * as tool from './tool'
import * as dialog from './dialog'
import * as wechat from './wechat'
import * as runtime from './runtime'
import * as form from './form'
import * as auth from './auth'
import * as oss from './oss/index'
import * as hook from './hook'

import ext from './ext/index'

export const qly = {
    env,
    config,
    tool,
    dialog,
    wechat,
    runtime,
    page,
    http,
    storage,
    form,
    crypto,
    api,
    update,
    auth,
    oss,
    hook,
    ...ext.export
}

export default {
    install(Vue) {
        
        // 挂载全局对象
        Vue.config.globalProperties.qly = qly
        
        // 处理本地地址
        const replaceHost = (url, origin) => {
            const protocols = ['http://', 'https://']
            for (const protocol of protocols) {
                const remoteOrigin = protocol + __DEV_REMOTE_IP__
                if (url.startsWith(origin)) {
                    return remoteOrigin + url.substring(origin.length)
                }
            }
            return url
        }
        env.baseUrl = replaceHost(env.baseUrl, 'http://localhost')
        env.baseUrl = replaceHost(env.baseUrl, 'http://127.0.0.1')
        
        // 处理h5跨域
        // #ifdef H5
        const envUrl = new URL(env.baseUrl)
        if (envUrl.origin !== window.origin) {
            env.baseUrl = env.baseUrl.replace(envUrl.origin, window.origin + '/proxy')
        }
        // #endif
        
        // 处理$once重复注册问题
        const once = {}
        const uni$once = uni.$once
        uni.$once = (eventName, callback) => {
            if (once[eventName]) {
                uni.$off(eventName, once[eventName])
                delete once[eventName]
            }
            once[eventName] = callback
            return uni$once(eventName, callback)
        }
        
        // 处理h5支付
        // #ifdef H5
        const parser = new DOMParser()
        const providerHandler = {
            alipay: options => {
                uni.showLoading({
                    title: '支付中...',
                })
                const vDocument = parser.parseFromString(options.orderInfo, 'text/html')
                const form = vDocument.getElementsByTagName('form')[0]
                form.target = '_blank'
                document.body.appendChild(form)
                uni.$once('onAppShow', () => {
                    uni.hideLoading()
                    options.success && options.success()
                })
                form.submit()
                form.remove()
            }
        }
        uni.requestPayment = options => {
            const handler = providerHandler[options.provider]
            if (handler == null) {
                options.fail && options.fail({
                    errMsg: '支付方式不支持'
                })
            } else {
                handler(options)
            }
        }
        // #endif
        
        // 处理h5保存图片
        // #ifdef H5
        uni.saveImageToPhotosAlbum = options => {
            const element = document.createElement("a")
            element.download = options.fileName || new Date().getTime() + '.png'
            element.href = options.filePath
            document.body.appendChild(element)
            element.click()
            element.remove()
            options.success && options.success({
                path: null
            })
        }
        // #endif
        
        ext.install(Vue)
        
    }
}
